//
//  EmoticonPackage.swift
//  表情键盘
//
//  Created by 叶子韬韬 on 2017/5/10.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {

    var emoticons:[Emoticon] = [Emoticon]()
    
    init(id:String) {
        super.init()
     
        // 1.最近分组
        if id == "" {
            addEmptyEmoticon(true)
            return
        }
        
        //根据id拼接plist的路径
        let plistPath = Bundle.main.path(forResource: "\(id)/info.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        
        //根据路径读数据
        let array = NSArray(contentsOfFile: plistPath)! as! [[String : String]]
        
        var index = 0
        
        for var dict in array {
            
            if let png = dict["png"] {
                dict["png"] = id + "/" + png
            }
            
            emoticons.append(Emoticon(dict:dict))
            
            index += 1
            
            if index == 20 {
                index = 0
                // 添加删除表情
                emoticons.append(Emoticon(isRemove:true))
            }
            
            
        }
        
        
        // 5.添加空白表情
        addEmptyEmoticon(false)
        
    }
    
    internal func addEmptyEmoticon(_ isRecently : Bool) {
        let count = emoticons.count % 21
        if count == 0 && !isRecently {
            return
        }
        
        for _ in count..<20 {
            emoticons.append(Emoticon(isEmpty: true))
        }
        
        emoticons.append(Emoticon(isRemove: true))
    }

    
}
